package com.example.haojiutao.controller.admin;

import com.example.haojiutao.common.Result;
import com.example.haojiutao.model.domain.GoodsType;
import com.example.haojiutao.service.GoodsTypeService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * @Description: 商品种类控制层
 * @Author: ma_lihong
 * @CreateTime: 2024/1/18 16:53
 */
@Slf4j
@RestController
public class GoodsTypeAdminController {

    @Autowired
    private GoodsTypeService goodsTypeService;

    /*
     * @Description: 添加商品种类
     * @Description: 添加商品种类
     * @Author: ma_lihong
     * @CreateTime: 2024/1/18 14:54
     * @param: goodsType
     * @return: com.example.haojiutao.common.Result
     */
    @PostMapping("/admin/goodstype")
    public Result insertType(@RequestBody  GoodsType goodsType){
        log.info("添加商品种类：{}",goodsType);
        goodsType.setUpdateTime(new Date());
        return goodsTypeService.insertType(goodsType);
    }

    /*
     * @Description: 删除商品种类
     * @Author: ma_lihong
     * @CreateTime: 2024/1/18 16:29
     * @param: goodsType
     * @return: com.example.haojiutao.common.Result
     */
    @DeleteMapping("/admin/goodstype/{typeId}")
    public Result deleteGoods(@PathVariable Integer typeId){
        return goodsTypeService.deleteById(typeId);

    }
    /*
     * @Description:根据parentId查询
     * @Author: ma_lihong
     * @CreateTime: 2024/1/19 15:12
     * @param: goodsType
     * @return: com.example.haojiutao.common.Result
     */
    @GetMapping("/admin/goodstype/{parentId}")
    public Result selectById(@PathVariable Integer parentId){
        log.info("查看父类型");
        GoodsType goodsType = new GoodsType();
        GoodsType goodsType1 = goodsTypeService.selectByParentId(parentId);
        return Result.success("查询成功！",goodsType);
    }
    /*
     * @Description:根据id查询
     * @Author: ma_lihong
     * @CreateTime: 2024/1/19 15:12
     * @param: goodsType
     * @return: com.example.haojiutao.common.Result
     */
    @GetMapping("/admin/goodstype/{typeId}")
    public Result selectByParentId(@PathVariable Integer parentId){
        log.info("查看单个商品类型");
        GoodsType goodsType = new GoodsType();
        GoodsType goodsType1 = goodsTypeService.selectById(parentId);
        return Result.success("查询成功！",goodsType);
    }
    /*
     * @Description: 列表查询
     * @Author: ma_lihong
     * @CreateTime: 2024/1/20 11:06
     * @param: gt
     * @return: com.example.haojiutao.common.Result
     */
//    @CrossOrigin(origins = {"http://localhost:5173"}, allowCredentials = "true")
    @GetMapping("/admin/goodstypelist")
    public Result selectByList(GoodsType gt){
        log.info("列表查询");
        List<GoodsType> types = goodsTypeService.selectByList(gt);
        log.info("商品类型========",types);
        PageInfo<GoodsType> pageInfo = new PageInfo<>(types);
        return Result.success("查询成功",pageInfo);

    }
    @GetMapping("/admin/goodstypelist/parent")
    public Result selectlListByParent(GoodsType gt){
        log.info("列表查询");
        List<GoodsType> types = goodsTypeService.selectlListByParent(gt);
        log.info("商品类型========",types);
        PageInfo<GoodsType> pageInfo = new PageInfo<>(types);
        return Result.success("查询成功",pageInfo);

    }
}
